package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.dto.input.BoreholeConditionInputDto;
import com.b511.swpu.cementbackend.dto.output.JsonDto;
import com.b511.swpu.cementbackend.entity.Sys_Module_Data;
import com.b511.swpu.cementbackend.model.ModuleDataField;
import com.b511.swpu.cementbackend.repository.Sys_Module_DataRepository;
import com.b511.swpu.cementbackend.service.DataBrowserService;
import com.b511.swpu.cementbackend.service.MetaDictService;
import com.b511.swpu.cementbackend.dto.input.MetaDictInputDto;
import com.b511.swpu.cementbackend.entity.Stratum;
import com.b511.swpu.cementbackend.repository.StratumRepository;
import com.b511.swpu.cementbackend.service.StratumService;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "地层信息", tags = {"地层信息"})
@RestController
@RequestMapping("/stratum")
public class StratumController {

    private final StratumRepository stratumRepository;

    private final StratumService stratumService;

    private final MetaDictService metaDictService;

    private final DataBrowserService dataBrowserService;

    private final Sys_Module_DataRepository module_dataRepository;

    public StratumController(StratumRepository stratumRepository, StratumService stratumService ,MetaDictService metaDictService,
                             DataBrowserService dataBrowserService, Sys_Module_DataRepository module_dataRepository) {
        this.stratumRepository = stratumRepository;
        this.stratumService = stratumService;
        this.metaDictService = metaDictService;
        this.dataBrowserService = dataBrowserService;
        this.module_dataRepository = module_dataRepository;
    }

    @GetMapping("/{num}/{size}")
    public R getAllStratum(@PathVariable int num, @PathVariable int size, @ModelAttribute BoreholeConditionInputDto boreholeConditionInputDto) {
        return stratumService.getStratumByCondition(boreholeConditionInputDto, PageRequest.of(num,size));
    }

    @PostMapping
    @Transactional
    public R saveStratum(@RequestBody Stratum stratum) {
        stratumRepository.save(stratum);
        return R.ok();
    }

    @GetMapping("/dict/child")
    public R getDictByName(@ModelAttribute @Validated MetaDictInputDto metaDictInputDto) {
        return metaDictService.getDictByName(metaDictInputDto.getLabel());
    }

    // 传入module_name以及分页信息，查询数据
    @GetMapping("/dataBrowser/{name}/{num}/{size}")
    public R getPorePressureByCondition(@PathVariable String name,@PathVariable int num, @PathVariable int size, @ModelAttribute BoreholeConditionInputDto boreholeConditionInputDto) throws Exception {
        return dataBrowserService.getDataByCondition(boreholeConditionInputDto, name, PageRequest.of(num, size));
    }

    // 只查询BA_WELL的数据(构造sql的bug)
    @GetMapping("/dataBrowser/BA_WELL/{num}/{size}")
    public R getWellByCondition(@PathVariable int num, @PathVariable int size, @ModelAttribute BoreholeConditionInputDto boreholeConditionInputDto) throws Exception {
        return dataBrowserService.getWellByCondition(boreholeConditionInputDto, PageRequest.of(num, size));
    }

    @GetMapping("/dataBrowser/tableHead/{name}")
    public R getTableHead(@PathVariable String name){
        // TODO: 动态表头实现：根据模块名，获取动态数据
        List<JsonDto> listDto = new ArrayList<>();
        // 根据mpduleName查相关field，获取property和label
        if(name!="" && name!=null) {
            List<Sys_Module_Data> module_data = module_dataRepository.findByModuleName(name);
            // 遍历赋值
            for (Sys_Module_Data module : module_data) {
                JsonDto jsonDto = new JsonDto();
                jsonDto.setProperty(module.getFieldName());
                // TODO: label添加单位
                if (module.getFieldUnit()!=null){
                    jsonDto.setLabel(module.getLabel()+"(" + module.getFieldUnit() +")");
                }else{
                    jsonDto.setLabel(module.getLabel());
                }

                listDto.add(jsonDto);
            }
            return R.ok().putData(listDto);
        }
        return R.error("moduleName 输入错误!");
    }

}
